<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>更新页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css"/>
</head>
<body>

<form class="layui-form">

    <div class="mainBox">
        <div class="main-container mr-5">

            <div class="layui-form-item">
                <label class="layui-form-label required">覆盖站点</label>
                <div class="layui-input-block">
                    <div name="scope_site_id" id="scope_site_id" value=""></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">规则类型</label>
                <div class="layui-input-block">
                    <div name="rule_type" id="rule_type" value=""></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">规则判断</label>
                <div class="layui-input-block">
                    <div name="rule_compare" id="rule_compare" value=""></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">规则参数</label>
                <div class="layui-input-block">
                    <input type="text" name="rule_value" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">规则操作</label>
                <div class="layui-input-block">
                    <div name="rule_action" id="rule_action" value=""></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">规则优先级</label>
                <div class="layui-input-block">
                    <input type="text" name="sort" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">规则备注</label>
                <div class="layui-input-block">
                    <input type="text" name="notes" value="" class="layui-input">
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">启用</label>
                <div class="layui-input-block">
                    <input type="checkbox" id="rule_status" lay-filter="rule_status" lay-skin="switch"/>
                    <input type="text" style="display:none" name="rule_status" value="0"/>
                </div>
            </div>


        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>

</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script>

    // 相关接口
    const PRIMARY_KEY = "id";
    const SELECT_API = "/app/csauto/rules/select" + location.search;
    const UPDATE_API = "/app/csauto/rules/update";

    // 获取数据库记录
    layui.use(["form", "util", "popup"], function () {
        let $ = layui.$;
        $.ajax({
            url: SELECT_API,
            dataType: "json",
            success: function (res) {
                // 给表单初始化数据
                layui.each(res.data[0], function (key, value) {
                    let obj = $('*[name="' + key + '"]');
                    if (key === "password") {
                        obj.attr("placeholder", "不更新密码请留空");
                        return;
                    }
                    if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                    if (obj[0].nodeName.toLowerCase() === "textarea") {
                        obj.val(layui.util.escape(value));
                    } else {
                        obj.attr("value", value);
                    }
                });


                layui.use(["jquery", "xmSelect"], function () {
                    layui.$.ajax({
                        url: "/app/csauto/api/get?name=rule_type",
                        dataType: "json",
                        success: function (res) {
                            let value = layui.$("#rule_type").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#rule_type",
                                name: "rule_type",
                                initValue: initValue,
                                data: res.data,
                                value: "1",
                                model: {"icon": "hidden", "label": {"type": "text"}},
                                clickClose: true,
                                radio: true,
                            });
                            if (res.code) {
                                layui.popup.failure(res.msg);
                            }
                        }
                    });
                    layui.$.ajax({
                        url: "/app/csauto/api/get?name=rule_compare",
                        dataType: "json",
                        success: function (res) {
                            let value = layui.$("#rule_compare").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#rule_compare",
                                name: "rule_compare",
                                initValue: initValue,
                                data: res.data,
                                value: "1",
                                model: {"icon": "hidden", "label": {"type": "text"}},
                                clickClose: true,
                                radio: true,
                            });
                            if (res.code) {
                                layui.popup.failure(res.msg);
                            }
                        }
                    });
                    layui.$.ajax({
                        url: "/app/csauto/api/get?name=rule_action",
                        dataType: "json",
                        success: function (res) {
                            let value = layui.$("#rule_action").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#rule_action",
                                name: "rule_action",
                                initValue: initValue,
                                data: res.data,
                                value: "1",
                                model: {"icon": "hidden", "label": {"type": "text"}},
                                clickClose: true,
                                radio: true,
                            });
                            if (res.code) {
                                layui.popup.failure(res.msg);
                            }
                        }
                    });
                    layui.$.ajax({
                        url: "/app/csauto/api/get?name=site_out",
                        dataType: "json",
                        success: function (res) {
                            let value = layui.$("#scope_site_id").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#scope_site_id",
                                name: "scope_site_id",
                                initValue: initValue,
                                data: res.data,
                                value: "1",
                                tips: '默认不选择情况下全体站点生效',
                                filterable:true,
                                autoRow:true
                                // model: {"icon":"hidden","label":{"type":"text"}},
                                // clickClose: true,
                                // radio: true,
                            });
                            if (res.code) {
                                layui.popup.failure(res.msg);
                            }
                        }
                    });
                });
                // 字段 禁用 rule_status
                layui.use(["form"], function () {
                    layui.$("#rule_status").attr("checked", layui.$('input[name="rule_status"]').val() != 0);
                    layui.form.render();
                    layui.form.on("switch(rule_status)", function (data) {
                        layui.$('input[name="rule_status"]').val(this.checked ? 1 : 0);
                    });
                })

                // ajax产生错误
                if (res.code) {
                    layui.popup.failure(res.msg);
                }

            }
        });
    });

    //提交事件
    layui.use(["form", "popup"], function () {
        layui.form.on("submit(save)", function (data) {
            data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
            layui.$.ajax({
                url: UPDATE_API,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }
            });
            return false;
        });
    });

</script>

</body>

</html>
